package com.itheima.edu.info.managers.service;

import com.itheima.edu.info.managers.dao.StudentDao;
import com.itheima.edu.info.managers.domain.Student;

public class StudentService {
    StudentDao studentDao = new StudentDao();

    public boolean addStudent(Student stu) {
        //把数据传输给StudentDao
        //创建对象，对象调方法添加

        return studentDao.addStudent(stu);

    }

    public boolean isExists(String id) {
        //找Dao拿数据，然后遍历数组
        Student[] stu = studentDao.findAllStudent();
        //遍历数组获得每一个学生对象，判断学号是否存在
        boolean exists = false;
        for (int i = 0; i < stu.length; i++) {
            Student student = stu[i];
            if (student != null && student.getId().equals(id)) {
                exists = true;
                break;
            }
        }
        return exists;
    }

    public void deleteStudent(String delId) {
        studentDao.deleteStudent(delId);
    }

    public Student[] findAllStudent() {
        // 1. 调用库管对象的findAllStudent获取学生对象数组
        Student[] allStudent = studentDao.findAllStudent();
        // 2. 判断数组中是否有学生信息 (有: 返回地址,  没有: 返回null)
        // 思路: 数组中只要存在一个不是null的元素, 那就代表有学生信息
        boolean flag = false;
        for (int i = 0; i < allStudent.length; i++) {
            Student stu = allStudent[i];
            if(stu != null){
                flag = true;
                break;
            }
        }

        if(flag){
            // 有信息
            return allStudent;
        }else{
            // 没有信息
            return null;
        }
    }

    public void updateStudent(Student stu,String updateId) {
        studentDao.updateStudent(stu,updateId);

    }
}
